#!/usr/bash


set  -o  pipefail
if [ $# -lt 3 ];then
	echo $0 "<data> <ali-dir> <dest_dir>"
	exit -1
fi


stage=5
train=$1
train_ali=$2
dest_dir=$3




cmvned_train=$dest_dir/ali2pdfs
[ ! -d $cmvned_train ] && mkdir -p $dest_dir
mkdir -p $cmvned_train
cmvned_feats_and_states_labels=$cmvned_train/feats_labels.$$
mkdir $cmvned_feats_and_states_labels

chunk_dir=$cmvned_train/feats_and_state_labels
[ -d $chunk_dir ] || rm -rf $chunk_dir
mkdir $chunk_dir

mats_per_ark=16
frames_per_mat=128
left_context=14
right_context=14

#[ -d $cmvned_train ] && rm -rf $cmvned_train

if [ $stage -le 1 ];then
	sh kaldidata2torch.sh $train $train_ali $cmvned_train
fi

if [ $stage -le 2 ];then
	echo 'fix_data_dir'
	utils/fix_data_dir.sh $cmvned_train/online_cmvn_data
fi

if [ $stage -le 3 ];then
	echo 'convert-ali-kaldidata-fmt'
	[  -d $cmvned_feats_and_states_labels ] || rm -rf $cmvned_feats_and_states_labels
	python convert-ali-kaldidata-fmt.py $cmvned_train/online_cmvn_data $cmvned_train/ali_pdf.scp $cmvned_feats_and_states_labels
fi


if [ $stage -le 4 ];then
	echo 'fix_data_dir with ' $cmvned_feats_and_states_labels
	utils/fix_data_dir.sh $cmvned_feats_and_states_labels
fi




if [ $stage -le 5 ];then
	echo 'split_ali-data-to-chunk-parallel-acc, use parameters.'
	[ -d $chunk_dir ] || rm -rf $chunk_dir
	frame_length_file=$cmvned_feats_and_states_labels/utt2num_frames
	echo 'copy ' $cmvned_train/feats_lengths ' to ' $frame_length_file
	cp -f $cmvned_train/feats_lengths  $frame_length_file

	echo 'frame_length_file is ' $frame_length_file
	python split_ali-data-to-chunk-parallel-acc.py $cmvned_feats_and_states_labels $chunk_dir $left_context $right_context $mats_per_ark $frames_per_mat
fi


echo 'result in ' $chunk_dir


